package com.example.demo.shiro;

import com.example.demo.Untils.PachongUtil;
import com.example.demo.entity.UUser;
import com.example.demo.service.IUUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * Created by Administrator on 2017/10/31/031.
 */
public class MyShiroRealm extends AuthorizingRealm {
    private static final Logger logger= LoggerFactory.getLogger(MyShiroRealm.class);
    @Autowired
    IUUserService iuUserService;
    /**
     * 认证信息.(身份验证) : Authentication 是用来验证用户身份
     *
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(
            AuthenticationToken authcToken) throws AuthenticationException {
        System.out.println("身份认证方法：MyShiroRealm.doGetAuthenticationInfo()");
        UsernamePasswordToken  token = (UsernamePasswordToken ) authcToken;
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("nickname", token.getUsername());
        map.put("pswd",String.valueOf(token.getPassword()));
        UUser user = null;
        // 从数据库获取对应用户名密码的用户
        List<UUser> userList = iuUserService.selectByMap(map);
        if(userList.size()!=0){
            user = userList.get(0);
        }
        if (null == user) {
            logger.info("帐号或密码不正确！");
            System.out.println("帐号或密码不正确！");
            throw new AccountException("帐号或密码不正确！");

        }else if(user.getStatus()==0){
            /**
             * 如果用户的status为禁用。那么就抛出<code>DisabledAccountException</code>
             */
            logger.info("帐号已经禁止登录！");
            System.out.println("帐号已经禁止登录！");
            throw new DisabledAccountException("帐号已经禁止登录！");
        }else{
            //更新登录时间 last login time
            user.setLastLoginTime(new Date());
            iuUserService.updateById(user);
        }
        return new SimpleAuthenticationInfo(user, user.getPswd(), getName());
    }
    /**
     * 授权
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(
            PrincipalCollection principals) {
        logger.info("权限认证方法：MyShiroRealm.doGetAuthenticationInfo()");
        UUser user = (UUser) SecurityUtils.getSubject().getPrincipal();
        int userId = user.getId();
        SimpleAuthorizationInfo info =  new SimpleAuthorizationInfo();
        //根据用户ID查询角色（role），放入到Authorization里。
	/*Map<String, Object> map = new HashMap<String, Object>();
	map.put("user_id", userId);
	List<SysRole> roleList = sysRoleService.selectByMap(map);
	Set<String> roleSet = new HashSet<String>();
	for(SysRole role : roleList){
		roleSet.add(role.getType());
	}*/
        //实际开发，当前登录用户的角色和权限信息是从数据库来获取的，我这里写死是为了方便测试
        Set<String> roleSet = new HashSet<String>();
        roleSet.add("100002");
        info.setRoles(roleSet);
        //根据用户ID查询权限（permission），放入到Authorization里。
	/*List<SysPermission> permissionList = sysPermissionService.selectByMap(map);
	Set<String> permissionSet = new HashSet<String>();
	for(SysPermission Permission : permissionList){
		permissionSet.add(Permission.getName());
	}*/
        Set<String> permissionSet = new HashSet<String>();
        permissionSet.add("权限添加");
        info.setStringPermissions(permissionSet);
        return info;
    }
}
